package cn.edu.sgu.www.mhxysy.service.server.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.server.ServerDTO;
import cn.edu.sgu.www.mhxysy.entity.server.Server;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.server.ServerMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.server.ServerPager;
import cn.edu.sgu.www.mhxysy.redis.repository.impl.ServerRepository;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.server.ServerService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ServerServiceImpl implements ServerService {

	private final ServerMapper serverMapper;
	private final ServerRepository serverRepository;

	@Autowired
	public ServerServiceImpl(ServerMapper serverMapper, ServerRepository serverRepository) {
		this.serverMapper = serverMapper;
		this.serverRepository = serverRepository;
	}

	@Override
	public void insert(ServerDTO serverDTO) {
		Server server = toEntity(serverDTO);

		server.setId(null);

		serverMapper.insert(server);
	}

	@Override
	public void deleteById(Integer id) {
		serverMapper.deleteById(id);
	}

	@Override
	public void updateById(ServerDTO serverDTO) {
		ObjectUtils.requireNonNull(serverDTO.getId(), CustomMessages.ID_NOT_NULL);

		Server server = toEntity(serverDTO);

		serverMapper.updateById(server);
	}

	@Override
	public List<Server> selectAll() {
		return serverRepository.selectList();
	}

	@Override
	public Server selectById(Integer id) {
		return serverRepository.selectById(id.toString());
	}

	@Override
	public List<Server> selectByAreaId(Integer areaId) {
		QueryWrapper<Server> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq(
				areaId != null,
				"area_id", areaId
		);

		return serverMapper.selectList(queryWrapper);
	}

	@Override
	public Page<Server> selectByPage(ServerPager pager) {
		QueryWrapper<Server> queryWrapper = new QueryWrapper<>();
		Page<Server> page = Pager.ofPage(pager);

		queryWrapper.like(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);
		queryWrapper.eq(
				pager.getAreaId() != null,
				"area_id", pager.getAreaId()
		);

		return serverMapper.selectPage(page, queryWrapper);
	}

	private Server toEntity(ServerDTO serverDTO) {
		Integer id = serverDTO.getId();
		String name  = serverDTO.getName();

		// 验证名称重复
		QueryWrapper<Server> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("name", name);
		queryWrapper.ne(id != null, "id", id);

		Long count = serverMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "服务器名重复！");
		}

		Server server = new Server();

		server.setId(id);
		server.setName(name);
		server.setAreaId(serverDTO.getAreaId());
		server.setOpenTime(serverDTO.getOpenTime());

		return server;
	}

}